import java.util.Deque;

public class KthLargest01 {
	private PriorityQueue<Integer> heap;
	private int k;
	private int[] tmp;

	public KthLargest01(int k, int[] nums) {
		this.k = k;
		heap = new PriorityQueue(new Comparator<Integer>() {
			@Override
			public int compare(Integer e1, Integer e2) {
				return -e1.compareTo(e2);
			}
		});
		for (int i = 0; i < nums.length; i++) {
			heap.offer(nums[i]);
		}
		tmp = new int[k - 1];
	}

	public int add(int val) {
		heap.offer(val);
		for (int i = 0; i < k - 1; ++i) {
			print(heap);
			tmp[i] = heap.poll();
		}
		int res = heap.peek();
		print(heap);
		for (int i = 0; i < tmp.length; i++) {
			heap.offer(tmp[i]);
		}
		print(heap);
		return res;
	}

	private void print(Queue<?> q) {
		Iterator<?> iterator = q.iterator();
		while (iterator.hasNext()) {
			System.out.print(iterator.next());
		}
		System.out.println();
	}
}
